package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Order;
import com.zyd.shiro.business.service.PakOrderService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakOrder;
import com.zyd.shiro.persistence.mapper.PakEnterLineMapper;
import com.zyd.shiro.persistence.mapper.PakOrderLineMapper;
import com.zyd.shiro.persistence.mapper.PakOrderMapper;
import com.zyd.shiro.persistence.mapper.PakStockMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakOrderServiceImpl implements PakOrderService {

    @Autowired
    private PakOrderMapper pakOrderMapper;

    @Autowired
    private PakOrderLineMapper pakOrderLineMapper;

    @Autowired
    private PakEnterLineMapper pakEnterLineMapper;

    @Autowired
    private PakStockMapper pakStockMapper;

    @Override
    public Order insert(Order order) {
        Assert.notNull(order, "对象不可为空！");
        order.getPakOrder().setUpdateTime(new Date());
        order.getPakOrder().setCreateTime(new Date());
        pakOrderMapper.insertSelective(order.getPakOrder());
        return order;
    }

    @Override
    public void insertList(List<Order> orders) {

    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakOrderMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Order order) {
        Assert.notNull(order, "对象不可为空！");
        order.setUpdateTime(new Date());
        return pakOrderMapper.updateByPrimaryKey(order.getPakOrder()) > 0;
    }

    @Override
    public boolean updateSelective(Order order) {
        Assert.notNull(order, "对象不可为空！");
        order.setUpdateTime(new Date());
        return pakOrderMapper.updateByPrimaryKeySelective(order.getPakOrder()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Order getByPrimaryKey(Long primaryKey) {
        PakOrder pakOrder = pakOrderMapper.selectByPrimaryKey(primaryKey);
        return null == pakOrder ? null : new Order(pakOrder);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Order getOneByEntity(Order test) {
        Assert.notNull(test, "对象不可为空！");
        PakOrder pakOrder = pakOrderMapper.selectOne(test.getPakOrder());
        return null == pakOrder ? null : new Order(pakOrder);
    }

    @Override
    public List<Order> listAll() {
        List<PakOrder> pakOrders = pakOrderMapper.selectAll();
        if (CollectionUtils.isEmpty(pakOrders)) {
            return null;
        }
        List<Order> orders = new ArrayList<>();
        for (PakOrder pakOrder : pakOrders) {
            orders.add(new Order(pakOrder));
        }
        return orders;
    }

    @Override
    public List<Order> listByEntity(Order entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Order> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakOrder> pakOrders = pakOrderMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakOrders)) {
            return null;
        }
        List<Order> orders = new ArrayList<>();
        for (PakOrder pakOrder : pakOrders) {
            orders.add(new Order(pakOrder));
        }
        PageInfo bean = new PageInfo<PakOrder>(pakOrders);
        bean.setList(orders);
        return bean;
    }
    @Override
    public void increase(PakOrder pakOrder) {
        pakOrderMapper.insert(pakOrder);
    }




}
